package com.jack.payment.service.strategy.wechat.impl;

import com.jack.payment.entity.business.RequestPayBo;
import com.jack.payment.entity.generate.config.ConfigPayWechatModel;
import com.jack.payment.entity.result.ResultPayVo;
import com.jack.payment.service.strategy.wechat.CreateWechatPayStrategy;
import com.wechat.pay.java.core.Config;


import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("Applet")
public class WechatAppletService implements CreateWechatPayStrategy {
    private Logger log = LoggerFactory.getLogger(WechatAppletService.class);

    @Override
    public ResultPayVo getWechatPaymentDetail(Config config,
                                              RequestPayBo requestPayBo,
                                              BigDecimal oneHundred,
                                              String webHook,
                                              ConfigPayWechatModel configPayWechatModel) {
        Integer thisPrice = requestPayBo.getAmount().multiply(oneHundred).intValue();

        JsapiService jsapiService = new JsapiService.Builder().config(config).build();
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(thisPrice);

        request.setMchid(configPayWechatModel.getMerchantId());
        request.setAppid(configPayWechatModel.getApplet());

        request.setOutTradeNo(requestPayBo.getOrderNo());
        request.setDescription(request.getDescription());
        request.setAttach(requestPayBo.getAttach());

        Payer payer = new Payer();
        payer.setOpenid(requestPayBo.getOpenId());

        request.setNotifyUrl(webHook);
        request.setAmount(amount);

        PrepayResponse response = jsapiService.prepay(request);
        return new ResultPayVo().builder()
                .paymentAmount(requestPayBo.getAmount())
                .paymentSignature(response.getPrepayId())
                .orderNo(requestPayBo.getOrderNo())
                .build();
    }
}
